package org.example.javachatroom.config;

import org.example.javachatroom.controller.TestWebSocketControler;
import org.example.javachatroom.controller.WebSocketController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestWebSocketControler testWebSocketControler;

    @Autowired
    private WebSocketController webSocketController;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //通过这个方法把刚才创建好的Handler类给注册到具体的路径上
        //此时当浏览器，websocket请求路径为“/test”的时候，就会调用到TestWebSocketController这个类里面的方法
        //        registry.addHandler(testWebSocketControler,"/test");
        registry.addHandler(webSocketController,"/WebSocketMessage")
                //通过这个HTTPSession拦截器，就可以把用户给HttpSession中添加的Attribute键值对往我们webSocketSession添加一份
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
